package RunableClass.useClass;





import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.String;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import RunableSt.connect;
import RunableSt.connect;
import java.io.StringReader;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author javadb.com
 */
public class ReadRank{
String symbolx;int Block[]=new int[12];
public static String[]sym=new String[5];String x ="";
static List<String>[] lst = new List[84];  
                   int countx=0;int type=0;
    /**
     * Reads text from a file line by line
     */
    public void readFromFile(String filename,String SET) throws SQLException {

        
        BufferedReader bufferedReader = null;
        String xc ="";
        String ya = null;
      
        
        try {
            //Construct the BufferedReader object
            bufferedReader = new BufferedReader(new StringReader(filename));
            String line = null;
          
            while ((line = bufferedReader.readLine()) != null) {
                 
                  int count1 = 0;int b[] = new int[1000];int c[] =new int[1000];int count2 = 0;
                //System.out.println(line.toString());
                            String linenumber = "";
                            String y ="";
                    if(line.contains("<div class=\"fl\" style=\"width:80px;padding-top:3px;text-align:left; padding-left:9px;\">")){
                       
                            for(int i=0;i<5;i++){
                                char xx=line.charAt(i);
                                
                                if(xx>='0'&&xx<='9'){
                                    linenumber+=line.substring(i, i+1);       
                                }        
                            }
                            ///1162
                                lst[countx]=new LinkedList<>();
                                 lst[countx].add(linenumber);
                                      
                            if(countx>0&&countx<=20){
                                  type=1;
                            }else if(countx>21&&countx<=41){;
                                    type=2;
                            }else if(countx>41&&countx<=62){
                                    type=3;
                            }else if(countx>62&&countx<=83){
                                    type=4;
                            }

                       if(line.startsWith((linenumber))){
                    
                        for(int i=0;i<line.length();i++){
                       char a= line.charAt(i);
                      if(a=='>'){
                          ++count1;
                         b[count1]=i;
                      }else if(a=='<'){
                           ++count2;
                          c[count2]=i;
                      }}
                       //  System.out.print("count1->"+ b[count1]+",");
                       //   System.out.print("count2->"+ b[count2]+",");
                   for(int i=0;i<100;i++){
                          if(b[i]!=0&&c[i]!=0){
                            // if(isDigit(line.substring(b[i]+1,c[i]))){
                                 if (i>1){
                                     ya=line.substring(b[i-1]+1,c[i-1]);
                                 }
                                 else{

                                 }
                               
                                 //if(!ya.isEmpty()||!ya.equals("")){
                                     //sym[0]=ya;
                                      
                                      //++countx;
                                  //   System.out.print(countx);
                                     
                                // }
                             //}
                        } }
                   lst[countx].add(ya);
                   lst[countx].add(String.valueOf(type));
                   
                        
                    ++countx;
                //   System.out.print("ya->"+ sym[0]+",");
                  //  x+="@"+y;
                    
                   
                    // System.out.println(Block[1]);
                   
                            ////////////////////////////////////
                    // System.out.print(""+y);
                    
                        }  
                       
                     
                    }
                   for(int xg=0;xg<countx;xg++){
                   for(int k=1;k<5;k++){
                        Block[k]=Integer.parseInt(lst[xg].get(0))+k;
                      if(!lst[xg].contains(String.valueOf(Integer.parseInt(lst[0].get(0))+k))){
                     //  lst[xg].add(String.valueOf(Integer.parseInt(lst[0].get(0))+k));
                       }
                           
                    
                    }
                 
                      
                   
                       for(int k=1;k<5;k++){
                       
                        if(line.startsWith(String.valueOf(Block[k])+"-->")){
                        for(int i=0;i<line.length();i++){
                       char a= line.charAt(i);
                      if(a=='>'){
                          ++count1;
                         b[count1]=i;
                      }else if(a=='<'){
                           ++count2;
                          c[count2]=i;
                      }}
                       //  System.out.print(String.valueOf(Block[k])+"-->count1->"+ b[count1]+",");
                        //  System.out.print(String.valueOf(Block[k])+"-->count2->"+ c[count2]+",");
                       //   System.out.println();
                   for(int i=0;i<100;i++){
                          if(b[i]!=0&&c[i]!=0){
                            // if(isDigit(line.substring(b[i]+1,c[i]))){   
                           //   System.out.println(b[i]+1+"-"+c[count2]+"-"+count2);
                              if(i>1&&k!=3){
                                      xc=line.substring(b[i]+1,c[i]);
                                   // sym[k]=xc;
                                    
                              }
                              if(k==3){
                                   xc=line.substring(b[i]+1,c[i]);
                               //   sym[k]=xc;
                                 // lst[xg].add(xc);
                              }
                             
                             }//}
                            
                          
                      
                        //  System.out.print( sym[k]);
                          
                           
                      
                        
                    //x+="@"+y;
                  if(k==5){
                      break;
                  }
                   
 }lst[xg].add(xc);
                           }
                     //  System.out.print(y);
                     }
                        
                   
                    }
                 

          
            }
           
        }
 catch (FileNotFoundException ex) {
            Logger.getLogger(ReadRank.class.getName()).log(Level.SEVERE, null, ex);
        }         catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            //Close the BufferedReader
            try {
                if (bufferedReader != null)
                    bufferedReader.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
    
     public static boolean isDigit(String s)   {
     boolean right = false;
      char c = 0;
      for (int index = 0; index < s.length(); index ++)  {
            c = s.charAt(index);
            if (c >= '0' || c <= '9'||c=='.')  {
            right = true;
           }
       }
      return right;
}


JTable JT;

 String[] Col1 = {"หลักทรัพย์","(มูลค่า'000 บาท)","ล่าสุด","เปลี่ยนแปลง","%เปลี่ยนแปลง"};
    
    public  void SetDb(String text,JTable JTable, int state){
        this.JT=JTable;
        try {
            DefaultTableModel model=new DefaultTableModel(Col1,0);
            new ReadRank().readFromFile(text,"");
            
            
                     connect a=   new connect();
                      String sqlsec="TRUNCATE TABLE stock_Ranking";
                      try {
                      a.stmt.execute(sqlsec);
                       a.CloseDB(); 
                        } catch (SQLException ex) {
                ex.printStackTrace();
            }
             for(int l=0;l<lst.length;l++){
                 if(l==0||l==21||l==42||l==63){
                     continue;
                 }
                 //if(!lst[l].equals(null)){
                    // System.out.println(lst[l]); 
                     
                     
                       // System.out.println(sqlsec);
                     
                  connect ab=   new connect();
             
                  // System.out.println(a.rs.getRow() +"---->");
              if(lst[l].get(2).toString().equals("1")&&state==1){
                  String x[]={lst[l].get(1).toString(),
                      lst[l].get(3).toString(),
                      Addport.SetWithStatus(lst[l].get(4),lst[l].get(5)).toString(),
                      Addport.Setcolor(lst[l].get(5)).toString(),
                      Addport.Setcolor(lst[l].get(6)).toString()};
  
                  model.addRow(x);
                  
              }else  if(lst[l].get(2).toString().equals("2")&&state==2){
                  String x[]={lst[l].get(1).toString(),
                      lst[l].get(3).toString(),
                      Addport.SetWithStatus(lst[l].get(4),lst[l].get(5)).toString(),
                      Addport.Setcolor(lst[l].get(5)).toString(),
                      Addport.Setcolor(lst[l].get(6)).toString()};
                  model.addRow(x);
              }else  if(lst[l].get(2).toString().equals("3")&&state==3){
                String x[]={lst[l].get(1).toString(),
                      lst[l].get(3).toString(),
                      Addport.SetWithStatus(lst[l].get(4),lst[l].get(5)).toString(),
                      Addport.Setcolor(lst[l].get(5)).toString(),
                      Addport.Setcolor(lst[l].get(6)).toString()};
                model.addRow(x);
              }else  if(lst[l].get(2).toString().equals("4")&&state==4){
                String x[]={lst[l].get(1).toString(),
                      lst[l].get(3).toString(),
                      Addport.SetWithStatus(lst[l].get(4),lst[l].get(5)).toString(),
                      Addport.Setcolor(lst[l].get(5)).toString(),
                      Addport.Setcolor(lst[l].get(6)).toString()};
                model.addRow(x);
              }
                        
          /*  //    String sql="INSERT INTO `stock_Ranking` ("+
                 //                  
                //                    "`SymbolName` ,"+
                                    "`Type` ,"+
                                    "`Value` ,"+
                                    "`LastChg` ,"+
                                    "`Chg` ,"+
                                    "`pChg` "+
                                    ")"+
                                    "VALUES ("+
                                    " '"+lst[l].get(1).toString()+"', '"+lst[l].get(2).toString()+"', '"+lst[l].get(3).toString()+"', "+lst[l].get(4).toString()+", "+lst[l].get(5).toString()+", "+lst[l].get(6).toString()+""+
                                    ");";
//System.err.println(sql);
                             
            try {
                ab.stmt.executeUpdate(sql);
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
                     */   
                        ab.CloseDB(); 
                    //    model.addRow(x);
               
          
               //  }
             
             //System.out.print(sym[l]+"@");
                        
              JT.setModel(model);
         JT.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
       
         JT.getColumnModel().getColumn(0).setResizable(false);
         JT.getColumnModel().getColumn(0).setPreferredWidth(200);
         JT.getColumnModel().getColumn(1).setResizable(false);
         JT.getColumnModel().getColumn(1).setPreferredWidth(200);
         JT.getColumnModel().getColumn(2).setResizable(false);
         JT.getColumnModel().getColumn(2).setPreferredWidth(200);
         JT.getColumnModel().getColumn(3).setResizable(false);
         JT.getColumnModel().getColumn(3).setPreferredWidth(200);
         JT.getColumnModel().getColumn(4).setResizable(true);
         JT.getColumnModel().getColumn(4).setPreferredWidth(200);
         DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();
        
dtcr.setHorizontalAlignment(SwingConstants.CENTER);
JT.getColumn("(มูลค่า'000 บาท)").setCellRenderer(dtcr);
JT.getColumn("ล่าสุด").setCellRenderer(dtcr);
JT.getColumn("เปลี่ยนแปลง").setCellRenderer(dtcr);
JT.getColumn("%เปลี่ยนแปลง").setCellRenderer(dtcr);


  
                        
         }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
                 
                 
    }
        
}

